package payrechargelogic

import (
	"context"
	model "gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/dao/model/recharge"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/pkg/xerrors"

	"gitee.com/liuxuezhan/ar-platform/assets-rpc/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-rpc/rpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeTypeUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRechargeTypeUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeTypeUpdateLogic {
	return &RechargeTypeUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// RechargeTypeUpdate 充值类型修改
func (l *RechargeTypeUpdateLogic) RechargeTypeUpdate(in *rpc.RechargeTypeUpdateRequest) (*rpc.RechargeTypeUpdateResponse, error) {
	var commonResp rpc.CommonResp
	// 创建更新数据的 map
	updateData := make(map[string]interface{})

	if in.SelectedIcon != "" {
		updateData["selected_icon"] = in.SelectedIcon
	}
	if in.UnselectedIcon != "" {
		updateData["unselected_icon"] = in.UnselectedIcon
	}
	if in.Name != "" {
		updateData["name"] = in.Name
	}
	if in.InternalName != "" {
		updateData["internal_name"] = in.InternalName
	}
	if in.Status != 0 {
		updateData["status"] = int32(in.Status)
	}
	if in.Sort != 0 {
		updateData["sort"] = in.Sort
	}

	// 检查是否有要更新的数据
	if len(updateData) == 0 {
		commonResp.Code = xerrors.ParameterError
		commonResp.Message = "No fields to update"
		return &rpc.RechargeTypeUpdateResponse{CommonResp: &commonResp}, nil
	}

	// 更新数据库
	err := model.NewRechargeTypeModel(l.ctx, l.svcCtx.Db).UpdateRechargeType(in.Id, updateData)
	if err != nil {
		commonResp.Code = xerrors.DBError
		commonResp.Message = "Database operation error"
		return &rpc.RechargeTypeUpdateResponse{CommonResp: &commonResp}, err
	}

	commonResp.Message = "Update successful"
	return &rpc.RechargeTypeUpdateResponse{CommonResp: &commonResp}, nil
}
